<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { FormSchema } from '@/components/Form'
import { Table, TableColumn } from '@/components/Table'
// import { formatToDate } from '@/utils/dateUtil'

import { useSearch } from '@/hooks/web/useSearch'
import { useTable } from '@/hooks/web/useTable'
import { ref, reactive, unref, computed } from 'vue'
import { GetDataListByTodayApi, getAllPartnerApi } from '@/api/user'
import { useI18n } from '@/hooks/web/useI18n'

import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()
// import dayjs from 'dayjs'
const { t } = useI18n()

// const lineOptionsData = computed<any>(() => {
//   return {
//     xAxis: {
//       data: Array.from({ length: 7 }, (v, i) => formatToDate(dayjs().subtract(7 - i, 'day'))),
//       boundaryGap: true,
//       axisTick: {
//         show: true
//       }
//     },
//     grid: {
//       left: 20,
//       right: 20,
//       bottom: 20,
//       top: 80,
//       containLabel: true
//     },
//     tooltip: {
//       trigger: 'axis',
//       axisPointer: {
//         type: 'cross'
//       },
//       padding: [5, 10]
//     },
//     yAxis: {
//       axisTick: {
//         show: false
//       }
//     },
//     legend: {
//       data: ['新增用户', '总广告收益', '激励视频收益', '激励广告数'],
//       top: 50
//     },
//     series: [
//       {
//         name: '新增用户',
//         smooth: true,
//         type: 'line',
//         data: dataList.value.map((item) => {
//           return item.newUserCount
//         }),
//         animationDuration: 2800,
//         animationEasing: 'cubicInOut'
//       },
//       {
//         name: '总广告收益',
//         smooth: true,
//         type: 'line',
//         data: dataList.value.map((item) => {
//           return (item.todayRevenue * 1).toFixed(2)
//         }),
//         animationDuration: 2800,
//         animationEasing: 'cubicInOut'
//       },
//       {
//         name: '激励视频收益',
//         smooth: true,
//         type: 'line',
//         data: dataList.value.map((item) => {
//           return (item.incentiveAdvertisingRevenue * 1).toFixed(2)
//         }),
//         animationDuration: 2800,
//         animationEasing: 'cubicInOut'
//       },
//       {
//         name: '激励广告数',
//         smooth: true,
//         type: 'line',
//         data: dataList.value.map((item) => {
//           return item.incentiveAdvertisingCount
//         }),
//         animationDuration: 2800,
//         animationEasing: 'cubicInOut'
//       }
//     ]
//   }
// })
const channel = ref<any>()
const getAllPartner = async () => {
  const { data } = await getAllPartnerApi()
  channel.value = data.map((item) => {
    return {
      label: item.userName,
      value: item.id
    }
  })
}
getAllPartner()
// Search
const { searchRegister, searchMethods } = useSearch()
const { getFormData: getSearchFormData } = searchMethods
const schema = computed<FormSchema[]>(() => [
  {
    field: 'userId',
    label: '渠道',
    component: 'Select',
    componentProps: {
      options: channel.value
    }
  }
])
//Table
const columns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index'
  },
  {
    field: 'userName',
    label: '合伙人'
  },
  {
    field: 'newUserCount',
    label: '新增用户数'
  },
  {
    field: 'todayRevenue',
    label: '总广告收益',
    slots: {
      default: ({ row }) => {
        return <span>￥{(row.todayRevenue * 1).toFixed(2)}</span>
      }
    }
  },
  {
    field: 'incentiveAdvertisingRevenue',
    label: '激励视频收益',
    slots: {
      default: ({ row }) => {
        return <span>￥{(row.incentiveAdvertisingRevenue * 1).toFixed(2)}</span>
      }
    }
  },
  {
    field: 'incentiveAdvertisingCount',
    label: '激励广告数'
  }
])
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const searchFormData = await getSearchFormData()
    const { currentPage, pageSize } = tableState
    const { data, count } = await GetDataListByTodayApi({
      pageNo: unref(currentPage),
      pageSize: unref(pageSize),
      appId: userStore.getAppId,
      ...searchFormData
    })
    return {
      list: data,
      total: count
    }
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList } = tableMethods
</script>

<template>
  <ContentWrap title="新增实时统计">
    <Search :schema="schema" @search="getList" @reset="getList" @register="searchRegister" />

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="columns"
      :data="dataList"
      row-key="id"
      :loading="loading"
      sortable
      headerAlign="center"
      align="center"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
</template>
